home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / S / SillyIcons / SillyIcons.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-07  |  6.7 KB  |  259 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------
  2. #
  3. ------------------------------------------------------------------------------*/
  4.  
  5. /*    
  6.     
  7.     purpose        To demonstrate use of the PlotIconID and PlotCIconHandle
  8.                 Toolbox calls documented in Macintosh Tech Note 306.
  9.                 See the TechNote for more information.  
  10.                 
  11.                 Requires System 7 (because that's where the routines are).
  12.                 Simply quits for lesser Systems.                              */
  13.  
  14. /*    WARNING        I am unable to safely use transforms other than ttNone
  15.                 and ttSelected with small icons.  The others crash disasterously
  16.                 (some only do so when the window is on a black-and-white
  17.                 device).     */
  18.                 
  19. /*    NOTE:        The program "steals" its icons from the System file.  If a needed
  20.                 icon is missing, the program simply beeps.
  21.                 There were no sicn which match the families I used, and no
  22.                 icm8 and icm4 icons at all, so the "mini" size is not exercised.
  23.                 The technique is the same...see the Tech Note.                    */
  24.  
  25. #include <Types.h>            /*  Based on the "SillyBalls" sample.  Some of these */
  26. #include <Memory.h>            /*    include files aren't needed.                     */
  27. #include <Quickdraw.h>
  28. #include <Fonts.h>
  29. #include <Events.h>
  30. #include <Menus.h>
  31. #include <Windows.h>
  32. #include <TextEdit.h>
  33. #include <Dialogs.h>
  34. #include <OSUtils.h>
  35. #include <ToolUtils.h>
  36. #include <SegLoad.h>
  37. #include <GestaltEqu.h>
  38. #include <IconPlotting.h>
  39.  
  40. /* Constants */
  41. #define BallWidth        20
  42. #define BallHeight        20
  43. #define BobSize            8        /* Size of text in each ball */
  44.  
  45. /* Globals */
  46. Rect    windRect;
  47.     
  48. /* Prototypes */
  49. void Initialize(void);
  50. void Icons(void);
  51.  
  52. /* 
  53. **    Main body of program SillyBalls
  54. */
  55. main()
  56. {
  57.     long version = 0;
  58.     (void) Gestalt(gestaltSystemVersion, &version);
  59.     if ( version >= 0x00000700) {
  60.         Initialize();
  61.         
  62.         Icons();
  63.         do {
  64.         } while (!Button());
  65.     }
  66. }
  67.  
  68. /* 
  69. **    Initialize everything for the program, make sure we can run
  70. */
  71. void Initialize()
  72. {
  73.     WindowPtr    mainPtr;
  74.     OSErr        error;
  75.     SysEnvRec    theWorld;
  76.     
  77.     /*
  78.     **    Test the computer to be sure we can do color.  
  79.     **    If not we would crash, which would be bad.  
  80.     **    If we can’t run, just beep and exit.
  81.     */
  82.     error = SysEnvirons(1, &theWorld);
  83.     if (theWorld.hasColorQD == false) {
  84.         SysBeep(50);
  85.         ExitToShell();                    /* If no color QD, we must leave. */
  86.     }
  87.     
  88.     /* Initialize all the needed managers. */
  89.     InitGraf(&qd.thePort);
  90.     InitFonts();
  91.     InitWindows();
  92.     InitMenus();
  93.     TEInit();
  94.     InitDialogs(nil);
  95.     InitCursor();
  96.  
  97.     /*
  98.     **    To make the Random sequences truly random, we need to make the seed start
  99.     **    at a different number.  An easy way to do this is to put the current time
  100.     **    and date into the seed.  Since it is always incrementing the starting seed
  101.     **    will always be different.  Don’t for each call of Random, or the sequence
  102.     **    will no longer be random.  Only needed once, here in the init.
  103.     */
  104.     GetDateTime((unsigned long*) &qd.randSeed);
  105.  
  106.     /*
  107.     **    Make a new window for drawing in, and it must be a color window.  
  108.     **    The window is full screen size, made smaller to make it more visible.
  109.     */
  110.     windRect = qd.screenBits.bounds;
  111.     InsetRect(&windRect, 50, 50);
  112.     mainPtr = NewCWindow(nil, &windRect, "\pBo3b Land", true, documentProc, 
  113.                         (WindowPtr) -1, false, 0);
  114.         
  115.     SetPort(mainPtr);                        /* set window to current graf port */
  116.     TextSize(BobSize);                        /* smaller font for drawing. */
  117. }
  118.  
  119. IconTransformType NextTransform(IconTransformType trans) {
  120.     switch (trans) {
  121.         case ttNone:
  122.             return ttDisabled;
  123.         
  124.         case ttDisabled:
  125.             return ttOffline;
  126.         
  127.         case ttOffline:
  128.             return ttOpen;
  129.         
  130.         case ttOpen:
  131.             return ttSelected;
  132.         
  133.         case ttSelected:
  134.             return ttSelectedDisabled;
  135.         
  136.         case ttSelectedDisabled:
  137.             return ttSelectedOffline;
  138.         
  139.         case ttSelectedOffline:
  140.             return ttSelectedOpen;
  141.         
  142.         case ttSelectedOpen:
  143.         default:
  144.             return ttNone;
  145.     }
  146. }
  147.  
  148. IconTransformType NextTint(IconTransformType tint) {
  149.     if ((tint += ttLabel1) > ttLabel7)
  150.         return ttLabel0;
  151.     else
  152.         return tint;
  153. }
  154.  
  155. /* 
  156. **    Icons: Draw the icons.
  157. */
  158. void Icons()
  159. {
  160.     Rect        bigRect;
  161.     Rect        smallRect;
  162.     Rect        theCIconRect;
  163.     Rect        tintRect;
  164.     Rect        smallTintRect;
  165.     Rect        tintCIconRect;
  166.     Rect        stretchRect;
  167.     Rect        crashRect;
  168.     OSErr         err;
  169.     CIconHandle theIcon     = NULL;
  170.     IconTransformType trans = ttNone;
  171.     IconTransformType tint  = ttLabel0;
  172.     SetRect(&bigRect,       20,  20,  53,  53);
  173.     SetRect(&smallRect,     20,  60,  37,  77);
  174.     SetRect(&theCIconRect,  20,  90,  53, 123);
  175.     SetRect(&tintRect,      20, 140,  53, 173);
  176.     SetRect(&smallTintRect, 20, 180,  37, 197);
  177.     SetRect(&tintCIconRect, 20, 210,  53, 243);
  178.     SetRect(&stretchRect,   20, 260,  85, 325);
  179.     
  180.     TextFont(1);
  181.     TextSize(9);
  182.     
  183.     do {
  184.         err = PlotIconID(&bigRect, 0, trans, -3994);
  185.         if (err)
  186.             SysBeep(7);
  187.  
  188.         /*    The transforms I'm not allowing here cause serious crashes in some or
  189.             all screen depths when applied to Small (or Mini) icons.  Don't do it.  */
  190.         if (trans == ttNone || trans == ttSelected) {
  191.             err = PlotIconID(&smallRect, 0, trans, -3994);
  192.             if (err)
  193.                 SysBeep(7);
  194.         }
  195.         else {
  196.             SetRect(&crashRect, smallRect.left,
  197.                                 smallRect.top + 4,
  198.                                 smallRect.left + 48,
  199.                                 smallRect.top + 20);
  200.             TextBox("Crash!", 6, &crashRect, teJustLeft);
  201.         }
  202.         
  203.         /*    There's no reason not to apply the transform to all tints, but some
  204.             of them are hard to see, so we'll make this row do double duty.     */
  205.         err = PlotIconID(&tintRect, 0, tint + ((trans > ttOpen) ? ttNone : trans), -3982);
  206.         if (err)
  207.             SysBeep(7);
  208.         
  209.         err = PlotIconID(&smallTintRect, 0, tint, -16509);
  210.         if (err)
  211.             SysBeep(7);
  212.  
  213.         if (theIcon == NULL)
  214.             theIcon = GetCIcon(-16396);
  215.         if (theIcon != NULL) {
  216.             err = PlotCIconHandle(&theCIconRect, 0, trans, theIcon);
  217.             if (err)
  218.                 SysBeep(7);
  219.         }
  220.         if (theIcon != NULL) {
  221.             err = PlotCIconHandle(&tintCIconRect, 0, tint, theIcon);
  222.             if (err)
  223.                 SysBeep(7);
  224.         }
  225.         OffsetRect(&bigRect,       50, 0);
  226.         OffsetRect(&smallRect,     50, 0);
  227.         OffsetRect(&tintRect,      50, 0);
  228.         OffsetRect(&smallTintRect, 50, 0);
  229.         OffsetRect(&theCIconRect,  50, 0);
  230.         OffsetRect(&tintCIconRect, 50, 0);
  231.         trans = NextTransform(trans);
  232.         tint  = NextTint(tint);
  233.     } while (trans != ttNone);
  234.     
  235.     err = PlotIconID(&stretchRect, 0, ttNone, -16509);
  236.     if (err)
  237.         SysBeep(7);
  238.     
  239.     TextFont(0);
  240.     TextSize(0);
  241.     MoveTo(20, 16);
  242.     DrawString("\pThese rows are drawn with the various transforms:");
  243.     MoveTo(20, 136);
  244.     DrawString("\pThese rows are drawn with the various tints (and 3 transforms):");
  245.     MoveTo(100, 276);
  246.     DrawString("\pDemonstration of PlotIconID and PlotCIconHandle per TN 306.");
  247.     MoveTo(100, 292);
  248.     DrawString("\pThe Tech Note says to use “align” of zero without definition.");
  249.     MoveTo(100, 308);
  250.     DrawString("\pAlign of 0 seems to be draw to fit…see the double-size");
  251.     MoveTo(100, 324);
  252.     DrawString("\pclipboard to the left.");
  253.     MoveTo(140, 356);
  254.     DrawString("\pClick the mouse to continue.");
  255.  
  256.     if (theIcon != NULL)
  257.         DisposCIcon(theIcon);
  258. }
  259.